fix: handle array-typed org/org_id in AuditEntry JSON unmarshaling#4204
fix: handle array-typed org/org_id in AuditEntry JSON unmarshaling#4204nghiack7 wants to merge 2 commits intogoogle:masterfrom
Conversation
GitHub's Enterprise audit-log API sometimes returns 'org' as a JSON array of strings and 'org_id' as a JSON array of integers, deviating from the documented scalar types. This caused an unmarshal error: json: cannot unmarshal array into Go struct field ... of type string Fix UnmarshalJSON to capture 'org' and 'org_id' as json.RawMessage, then decode each as scalar-or-array: - string arrays are joined with ', ' - int64 arrays use the first element Add two helpers (unmarshalStringOrStringArray, unmarshalInt64OrInt64Array) and table-driven tests covering scalar, single-element array, and multi-element array payloads. Fixes google#3488
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4204 +/- ##
==========================================
- Coverage 93.71% 93.67% -0.05%
==========================================
Files 209 209
Lines 19772 19805 +33
==========================================
+ Hits 18529 18552 +23
- Misses 1046 1051 +5
- Partials 197 202 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Cover the 5 missing + 6 partial lines flagged by Codecov: - Empty org_id array → nil OrgID (not panic) - Object-typed org → error from unmarshalStringOrStringArray - Object-typed org_id → error from unmarshalInt64OrInt64Array - Explicit JSON null for org and org_id → both fields nil, no error
|
Added tests to cover the error paths and edge cases flagged by Codecov (68.57% → should now be above 90%):
All existing tests still pass. |
Problem
The GitHub Enterprise audit-log API sometimes returns
orgas a JSON array of strings andorg_idas a JSON array of integers, deviating from the documented scalar types. This causesGetAuditLog/GetAuditLog(Enterprise) to return an unmarshal error:Reported on v66 and v69. See #3488.
Root cause
AuditEntry.UnmarshalJSONdelegates tojson.Unmarshalvia atype entryAlias AuditEntryalias. When the API returns"org": ["a", "b"]the decoder fails becauseOrgis typed*string.Fix
In
UnmarshalJSON, captureorgandorg_idasjson.RawMessagebefore the main decode pass, then normalise each:stringarrays are joined with", "(preserves all org names)int64arrays use the first element (mirrors how callers would use it)Two unexported helpers (
unmarshalStringOrStringArray,unmarshalInt64OrInt64Array) encapsulate the logic so the core unmarshal flow stays readable.Tests
Added table-driven tests in
orgs_audit_log_test.go:org_as_scalar_string"org":"myorg""myorg"org_as_single_element_array"org":["myorg"]"myorg"org_as_multi_element_array"org":["org1","org2","org3"]"org1, org2, org3"org_id_as_scalar"org_id":4242org_id_as_array"org_id":[42,43,44]42(first element)All existing audit-log tests continue to pass.